<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { getCode, login } from '@/api/login'
import type { Code, LoginParam, TokenInfo } from '@/api/login/types'
import router from '@/router'
import { ElMessage } from 'element-plus'
import { useTokenStore } from '@/stores/tokenStore'
import { useRoute } from 'vue-router'

// 验证码
const codeUrl = ref('')
// 表单
const loginFormRef = ref()
const loginForm = reactive<LoginParam>({
    username: '',
    password: '',
    code: '',
    uuid: ''
})
// 表单验证
const loginFormRules = ref({
    username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
    password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
    code: [{ required: true, message: '请输入验证码', trigger: 'blur' }]
})
// 持久化token
const tokenStore = useTokenStore()
// 路由
const route = useRoute()


// 获取验证码
const getVerifyCode = () => {
    getCode().then(res => {
        codeUrl.value = res.codeUrl
        loginForm.uuid = res.uuid
    }).catch(err => {
        console.log(err)
    })
}

// 登录
const confirm = () => {
    loginFormRef.value.validate((valid: boolean) => {
        if (valid) {
            login(loginForm).then(res => {
                tokenStore.setToken(res)
                router.push(route.query.dest? route.query.dest as string : '/')
            }).catch(err => {
                ElMessage.error(err.message)
                getVerifyCode()
            })
        }else{
            console.log('error submit!!')
            return false
        }
    })
}

// 取消
const cancel = () => {
    loginFormRef.value.resetFields()
    getVerifyCode()
}



onMounted(() => {
    getVerifyCode()
})
</script>

<template>
    <el-card class="login-card">
        <el-form :model="loginForm" :rules="loginFormRules" ref="loginFormRef" label-width="100px">
            <el-form-item label="用户名" prop="username">
                <el-input v-model="loginForm.username" placeholder="请输入用户名" />
            </el-form-item>
            <el-form-item label="密码" prop="password"> 
                <el-input type="password" v-model="loginForm.password" placeholder="请输入密码" />
            </el-form-item>
            <el-row :gutter="15">
                <el-col :span="16">
                    <el-form-item label="验证码" prop="code">
                        <el-input v-model="loginForm.code" placeholder="请输入验证码" />
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-image :src="codeUrl" class="code-image" />
                </el-col>
            </el-row>
            <el-form-item>
                <el-button type="primary" @click="confirm">登录</el-button>
                <el-button type="info" @click="cancel">取消</el-button>
            </el-form-item>
        </el-form>
    </el-card>
</template>

<style scoped>
.login-card {
    width: 400px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
}
.code-image {
    width: 100%;
    height: 100%;
}
</style>